package com.yenlien.traceyun.plant.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.RecallDao;
import com.yenlien.traceyun.business.entity.Recall;
import com.yenlien.traceyun.business.entity.RecallChain;
import com.yenlien.traceyun.plant.service.PlantRecallService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-12-17
 * @description:
 **/
@RestController
@RequestMapping(value = "/plant/recall")
public class PlantRecallController extends BaseController {

    @Autowired
    private PlantRecallService plantRecallService;

    @Autowired
    private RecallDao recallDao;


    @PostMapping("chain")
    public Object getRecallChain(@RequestBody Recall recall) throws Exception {

        return success(plantRecallService.getRecallChain(recall));
    }


    @PostMapping("/add")
    public Object recall(@RequestBody JSONObject params) throws Exception {
        Recall recall = params.getJSONObject("recall").toJavaObject(Recall.class);
        List<RecallChain> chains = params.getJSONArray("chains").toJavaList(RecallChain.class);
        return plantRecallService.save(recall, chains);
    }
}
